package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.AssessmentPaperPersonResultMapper;
import com.zhijian.medical.entity.po.AssessmentPaperPersonResult;
import com.zhijian.medical.entity.vo.request.AssessmentPaperPersonResultSearchVo;
import com.zhijian.medical.entity.vo.response.AssessmentPaperPersonResultVo;
import com.zhijian.medical.service.AssessmentPaperPersonResultService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 考核-试卷人员结果表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-02-01 10:35:15
 */
@Slf4j
@Service
public class AssessmentPaperPersonResultServiceImpl implements AssessmentPaperPersonResultService {

    @Autowired
    private AssessmentPaperPersonResultMapper mapper;

    @Override
    public AssessmentPaperPersonResult selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AssessmentPaperPersonResultVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByAssessmentPaperId(Long assessmentPaperId) {
        mapper.deleteByAssessmentPaperId(assessmentPaperId);
    }

    @Override
    public void deleteByAssessmentId(Long assessmentId) {
        mapper.deleteByAssessmentId(assessmentId);
    }

    @Override
    public void deleteByAssessmentPaperIdAndUserId(Long assessmentPaperId, Long userId) {
        mapper.deleteByAssessmentPaperIdAndUserId(assessmentPaperId, userId);
    }

    @Override
    public Long insert(AssessmentPaperPersonResult assessmentPaperPersonResult) {
        Date now = new Date();
        assessmentPaperPersonResult.setCreateTime(now);
        assessmentPaperPersonResult.setUpdateTime(now);
        return mapper.insert(assessmentPaperPersonResult) > 0 ? assessmentPaperPersonResult.getId() : 0L;
    }

    @Override
    public boolean update(AssessmentPaperPersonResult assessmentPaperPersonResult) {
        Date now = new Date();
        assessmentPaperPersonResult.setUpdateTime(now);
        return mapper.updateByPrimaryKey(assessmentPaperPersonResult) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AssessmentPaperPersonResult record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(AssessmentPaperPersonResultSearchVo assessmentPaperPersonResultSearchVo) {
        return mapper.count(assessmentPaperPersonResultSearchVo);
    }

    @Override
    public List<AssessmentPaperPersonResultVo> selectList(AssessmentPaperPersonResultSearchVo assessmentPaperPersonResultSearchVo) {
        return mapper.selectList(assessmentPaperPersonResultSearchVo);
    }

    @Override
    public AssessmentPaperPersonResultVo selectByAssessmentPaperIdAndUserIdAndQuestionId(Long assessmentPaperId, Long userId, Long questionId) {
        return mapper.selectByAssessmentPaperIdAndUserIdAndQuestionId(assessmentPaperId, userId, questionId);
    }

    @Override
    public Page<AssessmentPaperPersonResultVo> selectPage(AssessmentPaperPersonResultSearchVo assessmentPaperPersonResultSearchVo) {
        //先直接查总数
        int count = count(assessmentPaperPersonResultSearchVo);
        //分页
        Page<AssessmentPaperPersonResultVo> page = new Page<>(true, count, assessmentPaperPersonResultSearchVo.getPageNum(), assessmentPaperPersonResultSearchVo.getPageSize());
        assessmentPaperPersonResultSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AssessmentPaperPersonResultVo> responseVos = this.selectList(assessmentPaperPersonResultSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AssessmentPaperPersonResult> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public Integer selectRightCountByAssessmentPaperIdAndUserId(Long assessmentPaperId, Long userId) {
        return mapper.selectRightCountByAssessmentPaperIdAndUserId(assessmentPaperId, userId);
    }

    @Override
    public Integer selectSingleRightCountByAssessmentPaperIdAndUserId(Long assessmentPaperId, Long userId) {
        return mapper.selectSingleRightCountByAssessmentPaperIdAndUserId(assessmentPaperId, userId);
    }

}